import { defineStore } from "pinia";
import { getCategoryList } from "@/api/home";
import request from "@/api/request";
import mockRequest from "@/api/mock";
// slogan
interface listData {
  icon: string;
  desc: string;
}
type policyDescListData = listData[];
interface categoryListData {
  picUrl: string;
  extra: {
    operationResource: {
      categoryId: string;
    };
  };
  categoryName: string;
  targetUrl: string;
}
// type categoryList = categoryListData[]
// 热销榜
interface categoryHotSellModuleData {
  categoryList: categoryListData[];
  title: string;
}
// 10 新品首发
interface kingKongListItemData {
  picUrl: string;
  schemeUrl: string;
  text: string;
}
type kingKongModuleData = {
  kingKongList: kingKongListItemData[];
};
type homeDetailListData = {
  policyDescList: policyDescListData;
  categoryHotSellModule: categoryHotSellModuleData;
  kingKongModule: kingKongModuleData;
};
// navList
interface navListItemData {
    categoryId: string ;
    categoryName: string;
  }
interface homeStoreStateData {
  homeDetailList: homeDetailListData;
  navData:navListItemData[]
  // navHomeList:qqq[]
}

export const useHomeStore = defineStore({
  id: "home",
  state: (): homeStoreStateData => {
    return {
      homeDetailList: {
        policyDescList: [],
        categoryHotSellModule: {
          categoryList: [],
          title: "",
        },
        kingKongModule: {
          kingKongList: [],
        },
      },
    //   navHomeList: [],
      navData: [],
    };
    // categoryList:[]
  },
  actions: {
    // async getHomedetailData() {
    //     try {
    //         const res = await getHomedetail()
    //         console.log(res);

    //         this.homeDetailList = res
    //     } catch (error) {
    //         console.log('获取home页面数据失败');

    //     }

    // },
    async getHomeData() {
      try {
        const res = await request({ url: "xhr/index.json" });
        // console.log(res.data,'...')
        this.homeDetailList = res.data.data;
      } catch (error) {
        console.log(error);
      }
    },
    async getNavData() {
      try {
        const res = await mockRequest({ url: "/navData" });
        // console.log(res.data.navList);
        this.navData = res.data?.navList;
      } catch (error) {
        console.log(error);
      }
    },
    // async getCateList(id){
    //     try {
    //         const res = await request({url:`/item/list.json?categoryId=${id}`})
    //         console.log(res);
    //         this.cateList=res.global?.global

    //     } catch (error) {
    //         console.log(error)
    //     }
    // }
    // async getCategoryList(id: string | number) {
    //   try {
    //     const res = await getCategoryList(id);
    //     console.log(res);
    //     this.navHomeList = res.data;
    //   } catch (error) {
    //     console.log(error, "res");
    //   }
    // },
  },
  getters: {
    // policyDescList(){
    //     return this.homeDetailList.policyDescList
    // },
    policyDescList: (state) => state.homeDetailList.policyDescList,
    kingKongList: (state) => state.homeDetailList.kingKongModule?.kingKongList,
    categoryList: (state) =>
      state.homeDetailList.categoryHotSellModule?.categoryList,
    // categoryL1List: (state) => state.navHomeList.categoryL1List,
  },
});
